package FrequentItemSetMining;
import java.util.ArrayList;
import java.util.Collections;

import util.FileHandler;

public class GlobalAssociationRuleList {
	private static GlobalAssociationRuleList instance;
	private ArrayList<AssociationRule> rules;
	
	private GlobalAssociationRuleList() {
		rules = new ArrayList<AssociationRule>();
	}
	
	public static GlobalAssociationRuleList getInstance() {
		if (instance == null) {
			instance = new GlobalAssociationRuleList();
		}
		return instance;
	}

	public void addRule(AssociationRule ar) {
		rules.add(ar);
	}

	@SuppressWarnings("unchecked")
	public void printAllRules() {
		if(rules.size() == 0) return;
		Collections.sort(rules);
		Collections.reverse(rules);
		
		//write on file
		String s = "Association rules\nMinimum frequency:" + FrequentItemSetGenerator.MINFREQPERCENT +
				" Minimum confidence:" + FrequentItemSetGenerator.MINCONFPERCENT +
				" " + rules.size() + " rules\n";
		for(AssociationRule ar:rules) {
			s+=ar + "\n";
		}
		FileHandler.getInstance().write(s, "Rules-F"+FrequentItemSetGenerator.MINFREQPERCENT + "C"+ 
				FrequentItemSetGenerator.MINCONFPERCENT  +".txt");
		FileHandler.getInstance().addToGlobalFile("AssociationRules - Frequency:"+ FrequentItemSetGenerator.MINFREQPERCENT +
				" , Confidence:" + FrequentItemSetGenerator.MINCONFPERCENT + "  Items:" + rules.size()+ "\n");
		//print on screen
		/*
		System.out.println(rules.size() + "rules");
		for(AssociationRule ar:rules) {
			System.out.println(ar);
		}*/		
	}

	public void clear() {
		rules = new ArrayList<AssociationRule>();
	}

}
